package com.gua.jbs.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.gua.jbs.bean.UserBean;
import com.gua.jbs.pojo.Jbsult;
import com.gua.jbs.service.IUserService;
import com.gua.jbs.serviceImpl.UserService;
import com.gua.jbs.utility.ApplicationConstant;
import com.gua.jbs.utility.ApplicationException;
import com.gua.jbs.utility.CollectionUtil;
import com.gua.jbs.utility.StringUtil;

public class UserController extends BaseController{
	
	private static final Logger logger = Logger
			.getLogger(LoginController.class);
	
	private String message;	
	private IUserService userService=null;
	public UserController(){
		userService=new UserService();
	}
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String action = request.getParameter(ApplicationConstant.ACTION);
		String userId = request.getParameter(ApplicationConstant.USER_ID);
		String fName = request.getParameter(ApplicationConstant.FNAME);
		String password = request.getParameter(ApplicationConstant.PASSWORD);
		
				
		if (StringUtil.isNullOrEmpty(action)
				&& action.equals(ApplicationConstant.ADD_USER)) {
			boolean isUserAdded=false;
			UserBean userBean=new UserBean();
			try {
				isUserAdded=userService.addUser(userBean);
				if(isUserAdded){
					message=ApplicationConstant.USER_ADDED_SUCCUSSFUL;	
					
				}
				else{
					message=ApplicationConstant.USER_ADDED_UNSUCCUSSFUL;
					
				}
				
			} catch (ApplicationException e) {
				message=ApplicationConstant.USER_ADDED_UNSUCCUSSFUL;
				logger.info("Exception in Application UserController AddUser()--->"+e);
				e.printStackTrace();
			}
			catch (Exception e) {
				message=ApplicationConstant.USER_ADDED_UNSUCCUSSFUL;
				logger.info("Exception in UserController AddUser()--->"+e);
				e.printStackTrace();
			}			
		}		
		else if (StringUtil.isNullOrEmpty(action)
				&& action.equals(ApplicationConstant.CHANGE_PASSWORD)) {
			
			String newPWD = request.getParameter("NPWD");
			String reapeatedPWD = request.getParameter(ApplicationConstant.REPEATED_PWD);
			
			UserBean userBean=new UserBean();
						
			userBean.setUserId(userId);
			userBean.setfName(fName);
			userBean.setNewPassword(newPWD);
			userBean.setRepeatedPassword(reapeatedPWD);
			userBean.setPassword(password);
			try {
				boolean isChangePassword=userService.ChangePassword(userBean);
				if(isChangePassword){
					message=ApplicationConstant.PASSWORD_UPDATED_SUCCUSSFUL;	
					response.sendRedirect("/jewel/profiles");
				}
				else{
					message=ApplicationConstant.PASSWORD_UPDATED_UNSUCCUSSFUL;
					response.sendRedirect("/jewel/pages/jsp/changePassword.jsp?userId="+userId+"&message="+message);
				}
				
			} catch (ApplicationException e) {
				message=ApplicationConstant.PASSWORD_UPDATED_UNSUCCUSSFUL;
				logger.error("Exception in Application UserController ChangePassword()--->"+e.getMessage());
				e.printStackTrace();
			}
			catch (Exception e) {
				message=ApplicationConstant.PASSWORD_UPDATED_UNSUCCUSSFUL;
				logger.error("Exception in UserController ChangePassword()--->"+e.getMessage());
				e.printStackTrace();
			}
		
		}
		else if (StringUtil.isNullOrEmpty(action)
				&& action.equals(ApplicationConstant.CHANGE_USER_ROLE)) {
			
			boolean isChangeUserRole=false;
			String userRole=request.getParameter(ApplicationConstant.USER_ROLE);
			try {
				isChangeUserRole=userService.changeUserRole(userId,userRole);
				if(isChangeUserRole){
					message=ApplicationConstant.CHANGE_USER_ROLE_SUCCUSSFUL;
					response.sendRedirect("/jewel/profiles");
				}
				else{
					message=ApplicationConstant.CHANGE_USER_ROLE_UNSUCCUSSFUL;
					response.sendRedirect("/jewel/profiles");
				}
				
			}catch (ApplicationException e) {
				message=ApplicationConstant.CHANGE_USER_ROLE_UNSUCCUSSFUL;
				logger.error("Exception in Application UserController ChangeUserRole()--->"+e.getMessage());
				e.printStackTrace();
			}
			catch (Exception e) {
				message=ApplicationConstant.CHANGE_USER_ROLE_UNSUCCUSSFUL;
				logger.error("Exception in UserController ChangeUserRole()--->"+e.getMessage());
				e.printStackTrace();
			}
			
		}
		else if (StringUtil.isNullOrEmpty(action)
				&& action.equals(ApplicationConstant.DELETE_USER)) {
			
			boolean isDeleteUser=false;
			try {
				isDeleteUser=userService.deleteUser(userId);
				
				if(isDeleteUser){
					message=ApplicationConstant.DELETE_USER_SUCCUSSFUL;	
					response.sendRedirect("/jewel/profiles");
				}
				else{
					message=ApplicationConstant.DELETE_USER_UNSUCCUSSFUL;
					response.sendRedirect("/jewel/profiles");
				}
				
			}catch (ApplicationException e) {
				message=ApplicationConstant.DELETE_USER_UNSUCCUSSFUL;
				logger.error("Exception in Application UserController deleteUser()--->"+e.getMessage());
				e.printStackTrace();
			}
			catch (Exception e) {
				message=ApplicationConstant.DELETE_USER_UNSUCCUSSFUL;
				logger.error("Exception in UserController deleteUser()--->"+e.getMessage());
				e.printStackTrace();
			}	
		}
		else{
			
			List<Jbsult> list_users=new ArrayList<Jbsult>();
			
			try {
				list_users=userService.getUsers();
				boolean isListEmpty = CollectionUtil.isEmptyList(list_users);

				if (isListEmpty == false) {
					logger.info("Users Controller---" + list_users.size());
							
					request.setAttribute("list_users", list_users);
				}
				rd = request.getRequestDispatcher("/pages/jsp/profiles.jsp");
				rd.forward(request, response);
				
			}catch (ApplicationException e) {
				//message=ApplicationConstant.DELETE_USER_UNSUCCUSSFUL;
				logger.error("Exception in Application UserController getUsers()--->"+e.getMessage());
				e.printStackTrace();
			}
			catch (Exception e) {
				//message=ApplicationConstant.DELETE_USER_UNSUCCUSSFUL;
				logger.error("Exception in UserController getUsers()--->"+e.getMessage());
				e.printStackTrace();
			}
		}
	}
}
